package com.example.photoutil;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.camera_btn)
    Button cameraBtn;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.photo_btn)
    Button photoBtn;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    File img_file;
    Uri photouri;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            Bitmap bitmap=(Bitmap)msg.obj;
            imageView2.setImageBitmap(bitmap);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


    }


    @OnClick({R.id.camera_btn,R.id.photo_btn})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.camera_btn:
                if(checkPermission(Manifest.permission.CAMERA)){
                    takephoto();
                }else {
                    String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
                    ActivityCompat.requestPermissions(this, perms, 01);
                }
                break;
            case R.id.photo_btn:
                if(checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE)){
                    choosePhoto();
                }else {
                    String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
                    ActivityCompat.requestPermissions(this, perms, 02);
                }
                break;
        }
    }

    private void choosePhoto() {
        Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
        intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intentToPickPic, 2);
    }

    private boolean checkPermission(String permission){
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            //未授权，申请授权

            return false;
        } else {
            //已授权，拍照
            return true;
        }

    }
    private void takephoto() {
        Intent intentToTakePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        img_file = PhotoUtils.getImageFile(this);
        photouri = PhotoUtils.getFileUri(this, img_file);
        Log.i("Uri", "takephoto: " + photouri);
        intentToTakePhoto.putExtra(MediaStore.EXTRA_OUTPUT, photouri);

        startActivityForResult(intentToTakePhoto, 0);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
        switch (requestCode) {
            case 01:
                if (cameraAccepted) {
                    takephoto();
                } else {
                    //用户授权拒绝之后，友情提示一下就可以了
                    Toast.makeText(this, "请开启对应权限", Toast.LENGTH_SHORT).show();
                }

            break;
            case 02:
                if (cameraAccepted) {
                    choosePhoto();
                } else {
                    //用户授权拒绝之后，友情提示一下就可以了
                    Toast.makeText(this, "请开启对应权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (resultCode == RESULT_OK) {
                    Log.i(resultCode + "", "onActivityResult: success");

                    PhotoUtils.cropImageUri(MainActivity.this, photouri, img_file, 295, 413, 295, 413, 1);
                }
                break;
            case 1:
                if (resultCode == RESULT_OK) {
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(img_file)));

                    Glide.with(this).load(img_file).override(295,413).centerCrop().into(imageView);
//                    imageView2.setImageBitmap(PhotoUtils.showBitmap(img_file, 1));

                }
                break;
            case 2:
                PhotoUtils.cropGlide(MainActivity.this,data.getData(),295,413);

                break;
        }

    }


}
